/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author dell
 */
public class ParserJson {

    public static Map<String, String> getContent(String jsonString) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject object = JSONObject.fromObject(jsonString);
        for (Iterator<?> iter = object.keys(); iter.hasNext();) {
            String key = (String) iter.next();
            String value = object.get(key).toString();
            map.put(key, value);
        }
        return map;
    }

    public static Map[] ConversionArrayMap(String jsonString) throws Exception {
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        Map[] dataMap = new HashMap[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HashMap<String, String> map = new HashMap<String, String>();
            for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
                String key = (String) iter.next();
                String value = jsonObject.get(key).toString();
                map.put(key, value);
            }
            dataMap[i] = map;
        }
        return dataMap;
    }

    public static String getType(String jsonString) {
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        return jsonObject.get("data_type").toString();
    }

    public static long getAcceptTime(String jsonString) {
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        long ms = Long.parseLong(jsonObject.get("accept_time").toString());
        return ms;
    }

    public static List<Map<String, String>> getContentByRecord_list(String jsonString) throws Exception {
        JSONObject object = JSONObject.fromObject(jsonString);
//        JSONObject jsonObject = object.getJSONObject("record_list");
//        if (!jsonObject.isNullObject()) {
//            
//        }
        JSONArray dataArray = object.getJSONArray("record_list");
        List<Map<String, String>> mapArray = new ArrayList<Map<String, String>>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            Map<String, String> map = new HashMap<String, String>();
            for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
                String key = (String) iter.next();
                String value = jsonObject.get(key).toString();
                map.put(key, value);
            }
            mapArray.add(map);
        }
        return mapArray;
    }

}
